package com.busfm.provider;

import com.busfm.model.UserEntity;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.os.Bundle;

public class UserAccountManager {
    public static final String ACCOUNT_TYPE = "com.busfm";
    
    public static final String USERDATA_USER_ID = "user.data.uid";
    public static final String USERDATA_USER_EMAIL = "user.data.email";
    public static final String USERDATA_USER_NAME = "user.data.username";
    
    private static Account mainAccount = null;

    public static void initMainAccount(Context ctx) {
        AccountManager am = AccountManager.get(ctx);
        Account[] accounts = am.getAccountsByType(ACCOUNT_TYPE);
        if(accounts.length != 0)
            mainAccount = accounts[0];
    }
    
    public static Account getMainAccount(Context ctx){
        initMainAccount(ctx);
        return mainAccount;
    }

    public static int getUid(Context ctx) {
        initMainAccount(ctx);
        if (mainAccount == null) {
            return -1;
        } else {
            AccountManager am = AccountManager.get(ctx);
            String uidStr = am.getUserData(mainAccount, USERDATA_USER_ID);
            try {
                return Integer.parseInt(uidStr);
            } catch (Exception e) {
                return -1;
            }
        }
    }
    
    public static String getPassword(Context ctx) {
        initMainAccount(ctx);
        if (mainAccount == null) {
            return "";
        } else {
            AccountManager am = AccountManager.get(ctx);
            return am.getPassword(mainAccount);
        }
    }
    
    public static String getEmail(Context ctx) {
        initMainAccount(ctx);
        if (mainAccount == null) {
            return "";
        } else {
            AccountManager am = AccountManager.get(ctx);
            return am.getUserData(mainAccount, USERDATA_USER_EMAIL);
        }
    }
    
    public static String getNickName(Context ctx) {
        initMainAccount(ctx);
        if (mainAccount == null) {
            return "";
        } else {
            AccountManager am = AccountManager.get(ctx);
            return am.getUserData(mainAccount, USERDATA_USER_NAME);
        }
    }

    public static boolean isSignIn(Context ctx) {
        AccountManager am = AccountManager.get(ctx);
        Account[] accounts = am.getAccountsByType(ACCOUNT_TYPE);
        if(accounts.length == 0){
            return false;
        }
        return true;
    }
    
    public static UserEntity getUserData(Context ctx) {
        UserEntity userEntity = new UserEntity();
        initMainAccount(ctx);
        if (mainAccount != null) {
            AccountManager am = AccountManager.get(ctx);
            userEntity.setMemberID(am.getUserData(mainAccount, USERDATA_USER_ID));
            userEntity.setMemberEmail(am.getUserData(mainAccount, USERDATA_USER_EMAIL));
            userEntity.setMemberNickName(am.getUserData(mainAccount, USERDATA_USER_NAME));
        }
        return userEntity;
    }
    
    public static void saveUserData(Context ctx, UserEntity userEntity, String password){
        if(userEntity != null){
            initMainAccount(ctx);
            AccountManager am = AccountManager.get(ctx);
            Account[] accounts = am.getAccountsByType(ACCOUNT_TYPE);
            if(mainAccount == null || accounts.length == 0){
                mainAccount = new Account(userEntity.getMemberEmail(), ACCOUNT_TYPE);
                am.addAccountExplicitly(mainAccount, password, null);
            }
            am.setPassword(mainAccount, password);
            am.setUserData(mainAccount, USERDATA_USER_ID, userEntity.getMemberID());
            am.setUserData(mainAccount, USERDATA_USER_EMAIL, userEntity.getMemberEmail());
            am.setUserData(mainAccount, USERDATA_USER_NAME, userEntity.getMemberNickName());
        }
    }
    
    public static void removeAccount(Context ctx){
        initMainAccount(ctx);
        AccountManager am = AccountManager.get(ctx);
        Account[] accounts = am.getAccountsByType(ACCOUNT_TYPE);
        if(accounts.length != 0)
            for(int i = 0; i < accounts.length; i++){
                am.removeAccount(accounts[i], null, null);
            }
        mainAccount = null;//
    }
}
